<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="确定"
      @click-left="$emit('close'), message = '' "
      @click-right='suggest'
    />
    <!-- /导航栏 -->
    <div style="padding: 10px;">
      <van-field
        v-model="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="50"
        placeholder="请输入新昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  data () {
    return {
      message: ''
    }
  },
  methods: {
    async suggest () {
      this.$toast.loading({
        message: '保存中....',
        forbidClick: true,
        duration: 0
      })
      try {
        if (this.message === '') {
          this.$toast('用户名不能为空')
          return
        }
        await updateUserProfile({
          name: this.message
        })
        this.$toast('修改用户名成功')
      } catch (err) {
        console.dir(err)
        if (err.response && err.response.status === 409) {
          return this.$toast('用户名已存在')
        }
        this.$toast('更新失败')
      }
      this.isShowUpdateName = false
      this.$emit('close')
      this.message = ''
    }
  }
}
</script>

<style>
.van-popup {
  background: #f5f7f9;
}
</style>
